﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace BLL
{
    /// <summary>
    /// 商品类别, 业务逻辑处理
    /// </summary>
    public class ProductCategoryBase
    {
        /// <summary>
        /// 设备目录信息 添加
        /// </summary>
        /// <param name="categoryInfo">设备目录信息对象</param>
        public static string CreateNew(Model.ProductCategoryBase category)
        {
            category = ProcessUtility(category);
            if (category.Layer > 3)
            { 
                return "系统不允许添加超过三级的目录!"; 
            }
            else
            {
                return (new SQLServerDAL.ProductCategoryBase()).CreateNew(category); 
            }
        }

        /// <summary>
        /// 产品目录实体信息
        /// </summary>
        /// <param name="categoryObj">设备目录实体对象</param>
        /// <returns></returns>
        private static Model.ProductCategoryBase ProcessUtility(Model.ProductCategoryBase categoryObj)
        {
            DataTable dt = BLL.CommonBLL.Select("ProductCategoryBase", "*", " ORDER BY CreatedOn DESC");
            string parentId = categoryObj.ParentId;

            string strNumber = "", strTemp = ""; int intNumber;
            if (dt.Rows.Count == 0)
            {
                categoryObj.Layer = 1;
                categoryObj.CategoryNumber = "00001"; 
            }
            else
            {
                if (parentId == "{00000000-0000-0000-0000-000000000000}" || parentId == "00000000-0000-0000-0000-000000000000")
                {
                    categoryObj.Layer = 1;
                    for (int n = 1; n < 10000; n++)
                    {
                        strTemp = "";
                        intNumber = dt.Select("Layer = 1").Length + n;
                        for (int i = 0; i < 5 - intNumber.ToString().Length; i++)
                            strTemp += "0";
                        categoryObj.CategoryNumber = strNumber + strTemp + intNumber.ToString();
                        if (dt.Select("CategoryNumber = '" + categoryObj.CategoryNumber + "'").Length == 0)
                            break;
                    }
                }
                else
                {
                    DataRow dr = dt.Select(" CategoryId = '" + parentId + "'")[0];
                    categoryObj.Layer = Convert.ToInt32(dr["Layer"].ToString()) + 1;
                    strNumber = dr["CategoryNumber"].ToString().Trim();

                    for (int n = 1; n < 10000; n++)
                    {
                        strTemp = "";
                        intNumber = dt.Select("Layer = " + categoryObj.Layer + " and ParentID = '" + parentId + "'").Length + n;
                        for (int i = 0; i < 5 - intNumber.ToString().Length; i++)
                            strTemp += "0";
                        categoryObj.CategoryNumber = strNumber + strTemp + intNumber.ToString();
                        if (dt.Select(" CategoryNumber = '" + categoryObj.CategoryNumber + "'").Length == 0)
                            break;
                    }
                }
            }
            return categoryObj;
        }
 
        /// <summary>
        /// 修改目录
        /// </summary>
        /// <param name="subject">对象</param>
        /// <param name="intType">操作属性,0未修改父目录,1为修改父目录</param>
        public static string Modify(Model.ProductCategoryBase category, bool IsChangeParent)
        {
            category = ProcessUtility(category);
            if (category.Layer > 3)
            { 
                return "系统不允许添加超过三级的目录!";
            }
            else
            { 
                return (new SQLServerDAL.ProductCategoryBase()).Modify(category, IsChangeParent);
            }
        }
 
        /// <summary>
        /// 删除目录
        /// </summary> 
        public static string Delete(string categoryId, bool isLogical)
        { 
            return (new SQLServerDAL.ProductCategoryBase()).Delete(categoryId, isLogical);
        }

        /// <summary>
        /// 获取目录信息实体
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static Model.ProductCategoryBase GetObjById(string categoryId)
        {
            return (new SQLServerDAL.ProductCategoryBase()).GetObjById(categoryId); 
        }
    }
}
